﻿
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;

using Loli.Library.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //模板匹配
        private void btnCalc_Click(object sender, EventArgs e)
        {

            IntPtr handle = WinAPI.FindWindow(null, "MapleStory");

            WinAPI.ShowWindow(handle, 1);
            WinAPI.SetForegroundWindow(handle);
            Thread.Sleep(500);


            Image<Gray, Byte> sourceImage = new Image<Gray, Byte>(ScreenCapturer.Capture(handle));
            Image<Gray, Byte> templateImage = new Image<Gray, Byte>("未标题-3.bmp");

            DateTime dt = DateTime.Now;
            List<Point> list = this.TemplateMatching(sourceImage, templateImage, 0.85);
            label1.Text = (DateTime.Now - dt).TotalMilliseconds + "毫秒";

            if (list.Count <= 0)
            {
                label2.Text = "没找到";
            }
            else
            {
                label2.Text = "找到" + list.Count;
            }

            foreach (var item in list)
            {
                sourceImage.Draw(new Rectangle(item, new Size(templateImage.Width, templateImage.Height)), new Gray(5f), 3);
            }

            imgSource1.Image = sourceImage;


        }

        public List<Point> TemplateMatching(Bitmap source, Bitmap template, double score)
        {
            Image<Gray, Byte> sourceImage = new Image<Gray, byte>(source);
            Image<Gray, Byte> templateImage = new Image<Gray, byte>(template);

            List<Point> returnValue = TemplateMatching(sourceImage, templateImage, score);
            sourceImage.Dispose();
            template.Dispose();
            return returnValue;
        }

        public List<Point> TemplateMatching(Image<Gray, Byte> source, Image<Gray, Byte> template, double score)
        {
            List<Point> result = new List<Point>();
            Image<Gray, float> resultImage = source.MatchTemplate(template, Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED);

            float[, ,] matches = resultImage.Data;

            for (int x = 0; x < matches.GetLength(1); x++)
            {
                for (int y = 0; y < matches.GetLength(0); y++)
                {
                    double matchScore = matches[y, x, 0];

                    if (matchScore > score)
                    {
                        result.Add(new Point(x, y));
                    }
                }
            }

            return result;
        }

        private void button2_Click(object sender, EventArgs e)
        {

            Image<Gray, byte> template = new Image<Gray, byte>("Monster.mst");
            Image<Gray, Byte> sourceImage = new Image<Gray, Byte>("Maple0005.jpg");

            List<Point> list = EmguCV.TemplateMatching(sourceImage, template, 0.9);
            foreach (var item in list)
            {
                sourceImage.Draw(new Rectangle(item, new Size(template.Width, template.Height)), new Gray(5f), 3);
            }
            imgSource1.Image = sourceImage;
        }


    }
}